import { reactive, ref } from "vue"
import _ from "lodash"
import { getNewsCollect, getCompanyPatent, getCompanyAccs, getCompanyTips, queryArticle } from "../../../service/article"
export const useDetailHook = (props: any, type: '1' | '2') => {
  // 当前活跃tab
  const activeTab = ref("info")
  // 置顶资讯
  const topNews = ref<any[]>([])
  // 批次资讯
  const batchNews = ref<any[]>([])

  /**
   * 查询表单
   */
  interface NewsFormType {
    title: string
    proof: string
    completeTime: string
    batchs: string[]
    batchs2?: string
    characters: string[]
  }
  const newsForm = reactive<NewsFormType>({
    title: "",
    proof: "",
    completeTime: "",
    batchs: [],
    batchs2: "",
    characters: []
  })

  const newsLoading = ref(false)

  // 资讯性质数据
  const characters = ref([
    { dictLabel: "政策", dictValue: "8" },
    { dictLabel: "申报", dictValue: "9" },
    { dictLabel: "评审", dictValue: "10" },
    { dictLabel: "立项", dictValue: "11" },
    { dictLabel: "拨款", dictValue: "12" },
    { dictLabel: "监理", dictValue: "13" },
    { dictLabel: "验收", dictValue: "14" },
    { dictLabel: "公示", dictValue: "87237" },
    { dictLabel: "其他", dictValue: "95466" },
  ])
  // 项目批次数据
  const batchs = ref([
    { dictLabel: "2023", dictValue: "2023" },
    { dictLabel: "2022", dictValue: "2022" },
    { dictLabel: "2021", dictValue: "2021" },
    { dictLabel: "2020", dictValue: "2020" },
    { dictLabel: "2019", dictValue: "2019" },
    { dictLabel: "自定义", dictValue: "0" }
  ])
  // 选择方法
  const checkTag = (val: string) => {
    // 存在当前val则是取消操作
    if (newsForm.batchs.includes(val)) {
      // 找到当前值在列表中的位置
      const relIdx = newsForm.batchs.findIndex((batch) => batch === val)

      // 删除掉当前索引
      newsForm.batchs.splice(relIdx, 1)
    } else {
      newsForm.batchs.push(val)
    }
  }

  // 查询方法
  const queryNews = () => {
    // const params = _.cloneDeep(newsForm)
    newsLoading.value = true

    // 判断当前条件中是否存在
    // if (params.batchs2 && params.batchs.includes('0')) {
    //   params.batchs = params.batchs?.concat([params.batchs2 || "0"]).filter((item: any) => item != "0")
    // } else {
    //   params.batchs = params.batchs?.filter((item: any) => item != "0")
    // }

    // delete params.batchs2
    getNewsCollect({ ...newsForm, [type === '1' ? 'policyId' : 'orgId']: props?.info.id }).then(res => {
      const topArr = res.data?.topInformationList ? res.data.topInformationList : []
      const batchArr = res.data?.batchInformationList ? res.data.batchInformationList : []

      topNews.value = [
        ...topArr.map((item: any) => ({ type: 1, ...item })),
        ...batchArr.map((item: any, index: number) => {
          const type = index % 2 === 0 ? 2 : 1
          return item.informationList?.map((subItem: any) => ({ type, ...subItem }))
        }).flat(),]
    }).finally(() => {
      newsLoading.value = false
    })
  }

  // 查询默认资讯数据
  if (type == "1") {
    queryNews()
  }

  // 重置方法
  const resetNewsForm = () => {
    newsForm.batchs = []
    newsForm.characters = []
    newsForm.batchs2 = ""
    queryNews()
  }

  // 当前host
  const currHost = window.location.origin


  /**
   * 数据分析图
   */
  const dataPieList = ref<any[]>([
    { type: "5", label: "等级分析" },
    { type: "6", label: "部门分析" },
    { type: "7", label: "批次分析" },
    { type: "8", label: "金额分析" },
  ])


  /**
   * 关系图谱
   */
  const atlasList = ref<any[]>(
    type === "1" ?
      [
        { type: "1", label: "所属图谱" },
        { type: "2", label: "关联图谱" },
        { type: "3", label: "管理图谱" },
        { type: "4", label: "演变图谱" },
      ]
      :
      [
        { type: "5", label: "所属部门" },
        { type: "6", label: "主管政策" },
      ]
  )

  /**
   * 公共方法
   */
  // 打开新窗口
  const openNewPage = (path: 'analysis' | 'relationImg', type: string,seachType:String) => {
    console.log(props);

    window.open(`${location.origin}/${path}?id=${props.info.id}&type=${type}&seachType=`+seachType)
  }


  return { activeTab, topNews, batchNews, atlasList, newsForm, characters, batchs, checkTag, queryNews, resetNewsForm, newsLoading, currHost, dataPieList, openNewPage }
}

export const useCompanyHook = () => {
  const dataType = ref("专利")

  const patentData = ref()
  const accsData = ref()

  // 请求专利和软件著作权数据
  const getCompanyData = (companyId: any) => {
    Promise.all([getCompanyAccs({ companyId }), getCompanyPatent({ companyId })]).then(res => {
      const [data1, data2] = res

      patentData.value = data2.data
      accsData.value = data1.data
    })
  }

  // 请求提示文字
  let tipsText = ref("")
  const getTipsText = async (id: string) => {
    const result = await getCompanyTips(id)
    tipsText.value = result.msg;
  }

  const dataTypeChange = () => {
  }


  // 立项查询
  const projectData = ref()
  const projectDataTotal = ref()
  const queryProject = (id,name) => {
    // 打开抽屉，然后显示立项结果
    // 点击立项查询时初始化
    // 查询数据
    if(typeof id === 'string'){
      queryArticle({ searchType: "4",id,companyName:name}, { pageNum: 1, pageSize: 800 }).then(res => {
        projectData.value = res.rows
        projectDataTotal.value = res.total
      })
    }else{
      console.log('qweqw')

      queryArticle({ searchType: "4",...{...id.params}}, { pageNum: id.currentPage4, pageSize: id.pageSize4 }).then(res => {
        projectData.value = res.rows
        projectDataTotal.value = res.total
      })
    }
  }

  return {
    dataType,
    dataTypeChange,
    patentData,
    accsData,
    getCompanyData,
    getTipsText,
    tipsText,
    queryProject,
    projectData,
    projectDataTotal
  }
}
